package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.hos.vo.DoctorVo;
import io.renren.modules.hos.vo.params.SearchParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.DoctorDao;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.service.DoctorService;


@Service("doctorService")
public class DoctorServiceImpl extends ServiceImpl<DoctorDao, DoctorEntity> implements DoctorService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DoctorEntity> page = this.page(
                new Query<DoctorEntity>().getPage(params),
                new QueryWrapper<DoctorEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DoctorVo> selectDoctorByDep(String dep) {
        LambdaQueryWrapper<DoctorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorEntity::getDoctorDepartment, dep);
        List<DoctorEntity> doctorEntityList = baseMapper.selectList(queryWrapper);
        return copyList(doctorEntityList);
    }

    @Override
    public DoctorVo selectDoctorById(Long doctorId) {
        DoctorEntity doctorEntity = this.getById(doctorId);
        DoctorVo doctorVo = new DoctorVo();
        BeanUtils.copyProperties(doctorEntity, doctorVo);
        return doctorVo;
    }

    @Override
    public PageUtils searchByName(SearchParam searchParam) {
        List<DoctorVo> doctorVos = baseMapper.searchByName(searchParam.getKeyword());
        return new PageUtils(doctorVos, doctorVos.size(), searchParam.getPageSize(), searchParam.getPageIndex());
    }

    @Override
    public DoctorEntity selectByUserId(Long id) {
        LambdaQueryWrapper<DoctorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorEntity::getUserId, id);
        return baseMapper.selectOne(queryWrapper);
    }

    public DoctorVo copy(DoctorEntity doctorEntity){
        DoctorVo doctorVo = new DoctorVo();
        BeanUtils.copyProperties(doctorEntity, doctorVo);
        return doctorVo;
    }

    public List<DoctorVo> copyList(List<DoctorEntity> doctorEntityList){
        List<DoctorVo> doctorVoList = new ArrayList<>();
        for (DoctorEntity doctorEntity : doctorEntityList) {
            doctorVoList.add(copy(doctorEntity));
        }
        return doctorVoList;
    }

}